<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="format-detection" content="telephone=no">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="stylesheet" href="../../../static/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="../../../static/css/public.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form" lay-filter="formTest" id="form" style="width: 80%;">
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">学校</label>
			<div class="layui-input-block">
				<select name="sid" id="sid" lay-verify="required" lay-filter="sid">
					<option value="">请选择</option>
				</select>
			</div>
		</div>

		<div class="layui-form-item layui-row layui-col-xs12" style="display:none">
			<label class="layui-form-label">跟进人</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="ername" id="ername" lay-verify="required" placeholder="" disabled> <input type="hidden" class="layui-input" name="eid" id="eid" lay-verify="required" placeholder="">
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">联系人名</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="name" lay-verify="required" placeholder="">
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">学院</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="faculty" lay-verify="required" placeholder="">
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">电话</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="phone" lay-verify="required" placeholder="">
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">qq</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="qq" lay-verify="required" placeholder="">
			</div>
		</div>
		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">微信</label>
			<div class="layui-input-block">
				<input type="text" class="layui-input" name="wechat" lay-verify="required" placeholder="">
			</div>
		</div>

		<div class="layui-form-item layui-row layui-col-xs12">
			<label class="layui-form-label">备注</label>
			<div class="layui-input-block">
				<textarea placeholder="" class="layui-textarea" id="remarks" name="remarks"></textarea>
			</div>
		</div>

		<div class="layui-form-item layui-row layui-col-xs12">
			<div class="layui-input-block">
				<button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="submit">提交</button>
				<button type="button" class="layui-btn layui-btn-sm layui-btn-primary close-btn">取消</button>
			</div>
		</div>
	</form>
	<script type="text/javascript" src="../../../static/js/jquery-2.1.1.min.js"></script>
	<script type="text/javascript" src="../../../static/layui/layui.js"></script>
	<script type="text/javascript" src="../../../static/js/index.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/constant.js"></script>
	<script type="text/javascript" src="../../../static/custom/js/common.js"></script>
	<script type="text/javascript">
		layui.use([ 'form', 'laydate', 'layer' ], function() {
			var $ = layui.jquery;
			var form = layui.form;
			var layer = layui.layer;
			var laydate = layui.laydate;
			var selectN = layui.selectN;
			var toUpdateApi = "../../../advisory/counterpart/toUpdate";
			var insertApi = "../../../advisory/counterpart/insert";
			var updateApi = "../../../advisory/counterpart/update";
			var getEmployeeNameApi = "../../../advisory/counterpart/getEmployeeName";
			var getSchoolApi = "../../../getSchool";
			var getEmployeeApi = "../../../getEmployee";
			var insertMethod = "post";
			var updateMethod = "put";
			var url = insertApi;
			var method = insertMethod;
			
			var id = getUrlParam("id");
			if (id) {
				//更新
				$("#form").append("<input type='hidden' name='id' value='' >");
				method = updateMethod;
				url = updateApi;
				loadAjax( toUpdateApi + "/" + id, "get", null, function(response) {
					baseCallBack(response, function(response) {
						fomrVal(form, "formTest", response.data.Counterpart);
			  			$("#ername").val(response.data.employee.rname);
						//获取所有学校信息
						loadAjax( getSchoolApi, "get", null, function(response2) {
							baseCallBack(response2, function(response2) {
								if (response2.data && response2.data.length) {
									for(var  v  of  response2.data ){
										var selected = "";
										if(v.id==response.data.Counterpart.sid){
											selected="selected";
										}
										$("#sid").append('<option value="'+v.id+'" '+selected+'>'+v.name+'</option>');
										form.render("select"); // 更新全部
									}
								}
							});
						});
					});
				});
			}else{
				//获取所有学校信息
				loadAjax( getSchoolApi, "get", null, function(response) {
					baseCallBack(response, function(response) {
						if (response.data && response.data.length) {
							for(var  v  of  response.data ){
								$("#sid").append('<option value="'+v.id+'">'+v.name+'</option>');
								form.render("select"); // 更新全部
							}
						}
					});
				});
			}
			form.on('select(sid)', function(data){
				  var id= data.value;
				  //获取所有学校信息
				  loadAjax( getEmployeeNameApi+"/"+id, "get", null, function(response) {
					  baseCallBack(response, function(response) {
				  		  if (response.data) {
				  		     $("#eid").val(response.data.id);
				  			 $("#ername").val(response.data.rname);
				  		  }
				  	  });
				  });
				  form.render();
			});      
			form.on("submit(submit)", function(data) {
				loadAjax( url, method, $("#form").serialize(), function(
						response) {
					baseCallBack(response, parentReloadCallBack);
				});
				return false;
			})
		})
	</script>
</body>
</html>